using Glpt.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

namespace Glpt.Task
{
    /// <summary>
    /// 采集上交所行情信息类
    /// </summary>
    public class CollectMKTDT
    {
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public CollectMKTDT()
        {
        }
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="tradedate">日期</param>
        /// <param name="dest_file_name">文件</param>
        /// <param name="table_columns">列名清单</param>
        /// <returns>数据流</returns>
        public DataTableReader SetParams(string tradedate, string dest_file_name, string table_columns)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BUSI_DATE");
            Dictionary<string, string[]> ColList = new Dictionary<string, string[]>();
            string[] arr_table_columns = table_columns.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string col_value in arr_table_columns)
            {
                string key = "";
                string value = "";
                int idx = col_value.IndexOf(':');
                if (idx > 0)
                {
                    key = col_value.Substring(0, idx);
                    value = col_value.Substring(idx + 1);
                }
                else
                    value = col_value;
                string[] fields = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string field_name in fields)
                {
                    if (!dt.Columns.Contains(field_name))
                        dt.Columns.Add(field_name);
                }
                ColList.Add(key, fields);
            }

            int irow = 0;
            int icol = 0;
            int ibegin = 0;
            string BusiDate = "";
            string MDStreamID = "";
            int TotNumTradeReports = 0;
            byte[] buff = File.ReadAllBytes(dest_file_name);
            DataRow dr = dt.NewRow();
            for (int i = 0; i < buff.Length; i++)
            {
                if (buff[i] == '|' || buff[i] == 10)
                {
                    string val = "";
                    if (icol == 2 && MDStreamID.StartsWith("MD4"))//港股行情的中文名称
                    {
                        if (i - ibegin < 32) continue;
                        val = System.Text.Encoding.Unicode.GetString(buff, ibegin, i - ibegin);
                    }
                    else
                        val = DataFuns.DefaultEncoding.GetString(buff, ibegin, i - ibegin);
                    if (irow == 0)
                    {
                        if (icol == 3)
                            TotNumTradeReports = int.Parse(val);
                        else if (icol == 6)
                            BusiDate = val.Substring(0, 8);
                    }
                    else
                    {
                        if (icol == 0)
                            MDStreamID = val;
                        val = val.Replace('\0', ' ').Trim();
                        if (val.Length > 0)
                        {
                            if (ColList.ContainsKey(MDStreamID))
                                dr[ColList[MDStreamID][icol]] = val;
                            else if (ColList.ContainsKey(""))
                                dr[ColList[""][icol]] = val;
                        }
                    }
                    ibegin = i + 1;
                    icol++;
                    if (buff[i] == 10)
                    {
                        if (irow > 0)
                        {
                            if (irow <= TotNumTradeReports)
                            {
                                if (ColList.ContainsKey(MDStreamID) || ColList.ContainsKey(""))
                                {
                                    dr["BUSI_DATE"] = BusiDate;
                                    dt.Rows.Add(dr);
                                }
                            }
                            else
                                break;
                        }
                        dr = dt.NewRow();
                        irow++;
                        icol = 0;
                    }
                }
            }
            return dt.CreateDataReader();
        }
    }
}
